package com.zeny.coding.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zeny.coding.constants.CourseConstants;
import com.zeny.coding.dto.ChapterDto;
import com.zeny.coding.dto.PageDto;
import com.zeny.coding.entity.Chapter;
import com.zeny.coding.entity.ChapterExample;
import com.zeny.coding.mapper.ChapterMapper;
import com.zeny.coding.service.ChapterService;
import com.zeny.coding.service.SectionService;
import com.zeny.coding.util.SnowFlake;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <h2>章节服务</h2>
 *
 * @Author Zeny
 */
@SuppressWarnings("all")
@Service
public class ChapterServiceImpl implements ChapterService {

    @Autowired
    private ChapterMapper chapterMapper;

    @Autowired
    private SectionService sectionService;

    @Override
    public void list(PageDto<ChapterDto> pageDto, String courseId) {
        //分页
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        //设置条件
        ChapterExample chapterExample = new ChapterExample();
        chapterExample.createCriteria().andCourseIdEqualTo(courseId);
        chapterExample.setOrderByClause("sort");
        //查询结果
        List<Chapter> chapterList = this.chapterMapper.selectByExample(chapterExample);
        PageInfo<Chapter> pageInfo = new PageInfo<>(chapterList);
        //设置总条数
        pageDto.setTotal(pageInfo.getTotal());
        //封装成DTO传输
        List<ChapterDto> chapterDtoList = new ArrayList<>();
        for (Chapter chapter : chapterList) {
            ChapterDto chapterDto = new ChapterDto();
            BeanUtils.copyProperties(chapter, chapterDto);
            chapterDtoList.add(chapterDto);
        }
        //设置记录数
        pageDto.setList(chapterDtoList);
    }

    @Override
    public void save(ChapterDto chapterDto) {
        //DTO转~
        Chapter chapter = new Chapter();
        BeanUtils.copyProperties(chapterDto, chapter);
        //算法算法生成ID
        SnowFlake snowFlake = new SnowFlake(
                CourseConstants.dataCenterId,
                CourseConstants.machineId);
        chapter.setId(String.valueOf(snowFlake.nextId()));
        this.chapterMapper.insertSelective(chapter);
    }

    @Override
    public void update(ChapterDto chapterDto) {
        Chapter chapter = new Chapter();
        BeanUtils.copyProperties(chapterDto, chapter);
        this.chapterMapper.updateByPrimaryKeySelective(chapter);
    }

    @Override
    public ChapterDto findById(String id) {
        Chapter chapter = this.chapterMapper.selectByPrimaryKey(id);
        ChapterDto chapterDto = new ChapterDto();
        BeanUtils.copyProperties(chapter, chapterDto);
        return chapterDto;
    }

    @Transactional
    @Override
    public void deleteById(String id) {
        //根据大章id删除小节
        this.sectionService.deleteByChapterId(id);
        this.chapterMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    @Override
    public void deleteByCourseId(String courseId) {
        ChapterExample chapterExample = new ChapterExample();
        chapterExample.createCriteria().andCourseIdEqualTo(courseId);
        this.chapterMapper.deleteByExample(chapterExample);
    }

    @Override
    public List<Chapter> findByCourseId(String courseId) {
        ChapterExample example = new ChapterExample();
        example.createCriteria().andCourseIdEqualTo(courseId);
        example.setOrderByClause("sort");
        List<Chapter> chapters = this.chapterMapper.selectByExample(example);
        return chapters;
    }
}
